package com.relatev.minecraft.UWantRestart.Sponge;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

public class SPCommandManager implements CommandCallable {

    private static SPCommandManager Instance;

    public static void init() {
        Instance = new SPCommandManager();
        Sponge.getCommandManager().register(UWantRestartSP.MainPlugin, Instance, Arrays.asList("uwantrestart", "uwr"));
    }

    @Override
    public CommandResult process(CommandSource source, String astring) throws CommandException {
        String[] args = astring.split("\\s+");
        source.sendMessage(Text.of("§r------§aUWantRestart§r------"));
        if (astring.equals("")) {
            source.sendMessage(Text.of("§c/uwr help 查阅帮助"));
            return CommandResult.success();
        }
        if (args[0].equalsIgnoreCase("help")) {
            source.sendMessage(Text.of("§bhttps://www.relatev.com/forum.php?mod=viewthread&tid=2504"));
            source.sendMessage(Text.of("§bhelp 查阅帮助"));
            source.sendMessage(Text.of("§elist 自动重启条件表"));
            source.sendMessage(Text.of("§erestart <秒数> (提示信息) 在多少秒后重启"));
        }
        if (source.hasPermission("AntiAttack.admin")) {
            if (args[0].equalsIgnoreCase("list")) {
                source.sendMessage(Text.of("§b重启条件:"));
                source.sendMessage(Text.of("" + SPConfigManager.Conditions));
            }
            if (args[0].equalsIgnoreCase("restart")) {
                switch (args.length) {
                    case 1:
                        SPRestartTasker.restart("§c服务器即将重启...§a请稍后上线!");
                    case 2:
                        SPRestartTasker.restart("§c服务器即将重启...§a请稍后上线!", Integer.parseInt(args[1]));
                    case 3:
                        SPRestartTasker.restart(args[2].replaceAll("&", "§"), Integer.parseInt(args[1]));
                }
            }
        }
        return CommandResult.success();
    }

    @Override
    public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException {
        return Collections.emptyList();
    }

    @Override
    public boolean testPermission(CommandSource source) {
        return true;
    }

    @Override
    public Optional<Text> getShortDescription(CommandSource source) {
        return Optional.of(Text.of("UWantRestart Plugin Command"));
    }

    @Override
    public Optional<Text> getHelp(CommandSource source) {
        return Optional.of(Text.of("UWantRestart Plugin Command"));
    }

    @Override
    public Text getUsage(CommandSource source) {
        return Text.of("§c/uwr help 查阅帮助");
    }
}
